package com.dcbeida.mine;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.activity.sales.StoresalesPresenter;
import com.dcbeida.bean.MyClientListBean;
import com.dcbeida.bean.PatientList;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.model.SotresalesModel;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/8/31.
 */

public class PatientListPersenter implements PatientListContract.Persent {
    private SotresalesModel model;
    PatientListContract.View view;
    Activity activity;

    public PatientListPersenter(PatientListContract.View view, Activity activity) {
        this.view = view;
        this.activity = activity;
        model = SotresalesModel.newInstance();
    }

    @Override
    public void start() {

    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };

    @Override
    public void getPatientInfo(int type, String keyword) {
        model.querypatient(type, keyword, new QuerypatientCallBack(activity, PatientList.class,
                lis));
    }



    //药店销售-查询患者信息列表
    class QuerypatientCallBack<T> extends DialogCallback<T> {

        public QuerypatientCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof PatientList) {
                    if (((PatientList) data).getList().size() > 0) {
                        view.showPatientlist(((PatientList) data).getList());
                    } else {
                        view.showAddNewPatient();
                    }
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTelTip("数据未请求成功,请检查网络...");


        }
    }
}
